'This class was converted from C# code created by abi.exDream.com to VB.Net 2005/2008 class by Danny C. McNaught

#Region "Imports directives"
Imports System
Imports System.IO
Imports Microsoft.VisualBasic
Imports System.Collections
Imports System.ComponentModel
Imports DevExpress.CodeRush
Imports DevExpress.CodeRush.Core
Imports DevExpress.CodeRush.PlugInCore
Imports DevExpress.CodeRush.StructuralParser
#End Region

Namespace VBCodeCommenter
    ''' <summary>The Class Code rush helper
    ''' </summary>
    ''' <remarks>
    ''' 	<para><b>History:</b></para>
    ''' <list type="table">
    ''' 	<listheader>
    ''' 		<term>Term</term>
    ''' 		<description>Description</description>
    ''' 	</listheader>
    ''' 	<item>
    ''' 		<term>Developer: Danny C. McNaught<para>Developer Company: EDS</para> <para>Developer Phone: (919) 325-7311</para> <para>Developer Email: danny.mcnaught@eds.com</para> <para>Developed on: W2PZZ50H01</para> <para>Date: 9/10/2007</para><para>Time: 7:21:43 AM</para></term>
    ''' 		<description>Created XML Comment. </description>
    '''	''' 	</item>
    ''' </list>
    ''' </remarks>
    ''' <example>
    ''' </example>
    Public Class CodeRushHelper
#Region "Get parent section names"
        ''' <summary>
        ''' Get all parent section names when parent sections don't have
        ''' <para>their own code blocks.</para>
        ''' </summary>
        ''' <param name="section">Parameter section Delimiter capable block</param>
        ''' <returns>
        ''' </returns>
        ''' <remarks>
        ''' 	<para><b>History:</b></para>
        ''' <list type="table">
        ''' 	<listheader>
        ''' 		<term>Term</term>
        ''' 		<description>Description</description>
        ''' 	</listheader>
        ''' <item>
        ''' 		<term>Developer: Danny C. McNaught<para>Developer Company: EDS</para> <para>Developer Phone: (919) 325-7311</para> <para>Developer Email: danny.mcnaught@eds.com</para> <para>Developed on: W2PZZ50H01</para> <para>Date: 9/10/2007</para><para>Time: 7:21:53 AM</para></term>
        ''' 		<description>Created XML Comment. </description>
        ''' 	</item>
        ''' </list>
        ''' </remarks>
        ''' <example>
        ''' </example>
        Friend Shared Function GetParentSectionNames(ByVal section As DelimiterCapableBlock) As String
            Dim ret As String = ""
            If section.Parent IsNot Nothing AndAlso section.Parent.[GetType]().IsSubclassOf(GetType(DelimiterCapableBlock)) AndAlso DirectCast((section.Parent), DelimiterCapableBlock).HasDelimitedBlock = False AndAlso (section.Parent.ElementType = LanguageElementType.[If] OrElse section.Parent.ElementType = LanguageElementType.[Else] OrElse section.Parent.ElementType = LanguageElementType.[Do] OrElse section.Parent.ElementType = LanguageElementType.[While] OrElse section.Parent.ElementType = LanguageElementType.[For] OrElse section.Parent.ElementType = LanguageElementType.ForEach OrElse section.Parent.ElementType = LanguageElementType.Switch OrElse section.Parent.ElementType = LanguageElementType.[Try]) AndAlso (section.Parent.ElementType <> LanguageElementType.[Else] OrElse section.ElementType <> LanguageElementType.[Else]) Then
                ' Does parent has no own block? Only continue if not.
                ' Only count parent type if it is if, for, switch, etc.
                ' Update 2004-11-28: Don't add multiple else's, we can only have one
                ' else block above us, " // else else else if" doesn't look pretty
                ' Then add parent name (and even its parent names)
                ret += GetParentSectionNames(DirectCast((section.Parent), DelimiterCapableBlock))
                ret += (IIf(ret.Length > 0, " ", "")).ToString + section.Parent.ElementType.ToString().ToLower()
            End If
            ' if (section.Parent)
            Return ret
        End Function
        ' GetParentSectionNames(section)
#End Region
#Region "Get section name"
        ''' <summary>
        ''' Helper to get section name for commenting,
        ''' <para>we will not only get the method name (or whatever),</para>
        ''' <para>but also the type if it is important and the arguments.</para>
        ''' <para>e.g. "class CommenterPlugIn" or "GetSectionName(section)"</para>
        ''' </summary>
        ''' <param name="section">Parameter section Language element</param>
        ''' <param name="lines">Parameter lines <seealso cref =" system. array" /> As System. string ()</param>
        ''' <param name="showParameters">Parameter showParameters As <seealso cref= "System.Boolean"/></param>
        ''' <param name="showParametersOfExpressions">Parameter showParametersOfExpressions As <seealso cref= "System.Boolean"/></param>
        ''' <param name="showFullParameterNames">Parameter showFullParameterNames As <seealso cref= "System.Boolean"/></param>
        ''' <returns>
        ''' </returns>
        ''' <remarks>
        ''' 	<para><b>History:</b></para>
        ''' <list type="table">
        ''' 	<listheader>
        ''' 		<term>Term</term>
        ''' 		<description>Description</description>
        ''' 	</listheader>
        ''' <item>
        ''' 		<term>Developer: Danny C. McNaught<para>Developer Company: EDS</para> <para>Developer Phone: (919) 325-7311</para> <para>Developer Email: danny.mcnaught@eds.com</para> <para>Developed on: W2PZZ50H01</para> <para>Date: 9/10/2007</para><para>Time: 7:22:11 AM</para></term>
        ''' 		<description>Created XML Comment. </description>
        ''' 	</item>
        ''' </list>
        ''' </remarks>
        ''' <example>
        ''' </example>
        Friend Shared Function GetSectionName(ByVal section As LanguageElement, ByVal lines As String(), ByVal showParameters As Boolean, ByVal showParametersOfExpressions As Boolean, ByVal showFullParameterNames As Boolean) As String
            If section Is Nothing OrElse section.[GetType]().IsSubclassOf(GetType(DelimiterCapableBlock)) = False Then
                Return ""
            End If

            Dim codeBlock As DelimiterCapableBlock = DirectCast(section, DelimiterCapableBlock)
            Dim name As String = section.PathSegment
            ' If section is a namespace, class, struct or enum
            ' also add that element name, this gives more overview.
            If section.ElementType = LanguageElementType.[Namespace] OrElse section.ElementType = LanguageElementType.[Class] OrElse section.ElementType = LanguageElementType.[Interface] OrElse section.ElementType = LanguageElementType.Struct OrElse section.ElementType = LanguageElementType.[Enum] Then
                name = section.ElementType.ToString().ToLower() + " " + name
            End If

            ' If section has no name, use element name.
            If name.Length = 0 Then
                name = section.ElementType.ToString().ToLower()
            End If

            ' If using generics, CodeRush adds a `1 at the end, remove that!
            name = name.Replace("`1", "")

            ' Rename "propertyaccessorget" to "get"
            ' and "propertyaccessorset" to "set"
            If name.StartsWith("propertyaccessor") Then
                name = name.Remove(0, "propertyaccessor".Length)
            End If

            ' Also rename "usingstatement" to "using"
            If name.StartsWith("usingstatement") Then
                name = name.Remove("using".Length, "statement".Length)
            End If

            ' Check if we got some parent entries,
            ' this builds us some "if foreach if" combinations
            Dim parentSectionNames As String = GetParentSectionNames(codeBlock)
            If parentSectionNames.Length > 0 Then
                name = parentSectionNames + " " + name
            End If

            ' That's not all, we also want to see the number of Parameters
            ' for methods. But first check if we want Parameters at all?
            If showParameters AndAlso (showParametersOfExpressions OrElse section.ElementType = LanguageElementType.Method) Then
                ' Use this chars for splitting conditions in if, for, etc. statements
                Dim ConditionSplitChars As Char() = New Char() {" "c, "="c, "<"c, ">"c, "!"c, "("c, _
                 ")"c, Chr(10), Chr(13)}

                ' Is this an if statement?
                If section.ElementType = LanguageElementType.[If] AndAlso showFullParameterNames AndAlso section.[GetType]().IsSubclassOf(GetType(ConditionalExpression)) Then
                    ' Must be cast able to ConditionalExpression
                    ' Try to find out what we are "if"ing here ^^
                    Dim ifType As ConditionalExpression = DirectCast(section, ConditionalExpression)
                    ' Use only first line and first word in condition
                    Dim condition As String = ifType.Condition.ToString().Split(ConditionSplitChars)(0)
                    ' Only add if condition exists
                    If condition.Length > 0 Then
                        name += " (" + condition + ")"
                    End If
                ElseIf section.DetailNodeCount = 0 Then
                    ' if (section.ElementType)
                    ' No Parameters?
                    ' Add "()" only for methods
                    If section.ElementType = LanguageElementType.Method Then
                        name += "()"
                    End If
                Else
                    ' else if
                    If showFullParameterNames Then
                        ' Show Parameters with dots in brackets at the end.
                        Dim paramIndex As Integer = 0
                        Dim paramNames As String() = New String(section.DetailNodeCount - 1) {}
                        ' Doesn't work perfectly since new CodeRush version :(
                        For Each param As LanguageElement In section.DetailNodes
                            ' Update 2005-04-04: Only go through Param's, CodeRush
                            ' will now also put the method type into the detail nodes.
                            If param.[GetType]().IsSubclassOf(GetType(Expression)) = False AndAlso param.Name IsNot Nothing Then
                                paramNames(paramIndex) = param.Name
                                paramIndex += 1
                                ' Show a maximum of 3 Parameters
                                If paramIndex >= 3 Then
                                    Exit For
                                End If
                                ' if (param.Name)
                            End If
                        Next
                        ' foreach (param in DetailNodes)
                        ' Got no params yet for an if/for/while?
                        ' Then get it with line string!
                        If (paramIndex = 0 OrElse paramIndex = 1 AndAlso paramNames(0).Length <= 2) AndAlso section.StartLine - 1 >= 0 AndAlso section.StartLine - 1 < lines.Length Then
                            paramIndex = 0
                            If section.ElementType = LanguageElementType.[If] AndAlso lines(section.StartLine - 1).Trim().StartsWith("if") Then
                                paramNames = New String(0) {}
                                ' Remove "if" and then "("
                                Dim ifText As String = lines(section.StartLine - 1).Trim().Substring(2).Trim().Substring(1).Trim()
                                ' Still starting with '('? Then remove all '('
                                While ifText.StartsWith("(")
                                    ifText = ifText.Substring(1)
                                End While
                                paramNames(paramIndex) = ifText.Split(ConditionSplitChars)(0)
                                paramIndex += 1
                            ElseIf section.ElementType = LanguageElementType.[For] AndAlso lines(section.StartLine - 1).Trim().StartsWith("for") Then
                                ' if (section.ElementType)
                                paramNames = New String(0) {}
                                ' Remove "for" and then "("
                                paramNames(paramIndex) = lines(section.StartLine - 1).Trim().Substring(3).Trim().Substring(1).Trim().Split(ConditionSplitChars)(0)
                                paramIndex += 1
                            ElseIf section.ElementType = LanguageElementType.[While] AndAlso lines(section.StartLine - 1).Trim().StartsWith("while") Then
                                ' else if
                                paramNames = New String(0) {}
                                ' Remove "while" and then "("
                                paramNames(paramIndex) = lines(section.StartLine - 1).Trim().Substring(5).Trim().Substring(1).Trim().Split(ConditionSplitChars)(0)
                                paramIndex += 1
                                ' else if
                            End If
                        End If
                        ' if (paramIndex)
                        ' If this is foreach and we got 2 params show "params1 in param2"
                        If section.ElementType = LanguageElementType.ForEach AndAlso paramIndex = 2 Then
                            name += " (" + paramNames(0) + " in " + paramNames(1) + ")"
                        ElseIf paramIndex > 0 Then
                            ' if (section.ElementType)
                            ' For non-methods add a space (to differ from keywords)
                            name += (IIf(section.ElementType = LanguageElementType.Method, "", " ")).ToString + "("
                            For paramNum As Integer = 0 To paramIndex - 1

                                Dim paramName As String = paramNames(paramNum)
                                ' If paramName contains " . ", remove spaces
                                paramName = paramName.Replace(" . ", ".")
                                name += paramName + (IIf(paramNum < paramIndex - 1, ", ", "")).ToString
                            Next
                            ' for (paramNum)
                            name += ")"
                        ElseIf paramIndex = 0 AndAlso section.ElementType = LanguageElementType.Method Then
                            ' else if
                            ' This is a method, DetailNodes wasn't 0, but we didn't
                            ' found any useful detail nodes (only void return param),
                            ' just add (), only for methods of course.
                            name += "()"
                            ' else if
                        End If
                    Else
                        ' if (showFullParameterNames)
                        ' Show Parameters with dots in brackets at the end.
                        Dim dots As Integer = section.DetailNodeCount
                        ' Show a maximum of 3 dots
                        If dots > 3 Then
                            dots = 3
                        End If
                        ' For non-methods add a space (to differ from keywords)
                        name += (IIf(section.ElementType = LanguageElementType.Method, "", " ")).ToString + "(" + New String("."c, dots) + ")"
                        ' else
                    End If
                    ' else
                End If
            End If
            ' if (showParameters)
            Return name
        End Function
        ' GetSectionName(section, lines, showParameters)
#End Region
#Region "Is element constructor?"
        ''' <summary>The Sub Is constructor
        ''' </summary>
        ''' <param name="element">Parameter element Language element</param>
        ''' <returns>
        ''' </returns>
        ''' <remarks>
        ''' 	<para><b>History:</b></para>
        ''' <list type="table">
        ''' 	<listheader>
        ''' 		<term>Term</term>
        ''' 		<description>Description</description>
        ''' 	</listheader>
        ''' <item>
        ''' 		<term>Developer: Danny C. McNaught<para>Developer Company: EDS</para> <para>Developer Phone: (919) 325-7311</para> <para>Developer Email: danny.mcnaught@eds.com</para> <para>Developed on: W2PZZ50H01</para> <para>Date: 9/10/2007</para><para>Time: 7:22:26 AM</para></term>
        ''' 		<description>Created XML Comment. </description>
        ''' 	</item>
        ''' </list>
        ''' </remarks>
        ''' <example>
        ''' </example>
        Public Shared Function IsConstructor(ByVal element As LanguageElement) As Boolean
            ' If this is a method, check if its a constructor and add
            ' "Create " in front of the name, e.g. "Create shader effect"
            If element.ElementType = LanguageElementType.Method Then
                Dim locationParts As String() = element.Location.Split(New Char() {"."c})
                ' Check if last and last but one are equal, e.g.
                ' "TestCommenter.Form1.Form1" means we fond a constructor!
                Return locationParts.Length >= 2 AndAlso locationParts(locationParts.Length - 1) = locationParts(locationParts.Length - 2)
            End If
            ' if
            ' No it isn't
            Return False
        End Function
        ' IsConstructor(element)
#End Region
#Region "Return child element name"
        ''' <summary>
        ''' Helper for adding header comment to get class and namespace names.
        ''' </summary>
        ''' <param name="node">Parameter node Language element</param>
        ''' <param name="elementType">Parameter elementType Language element type</param>
        ''' <returns>
        ''' </returns>
        ''' <remarks>
        ''' 	<para><b>History:</b></para>
        ''' <list type="table">
        ''' 	<listheader>
        ''' 		<term>Term</term>
        ''' 		<description>Description</description>
        ''' 	</listheader>
        ''' <item>
        ''' 		<term>Developer: Danny C. McNaught<para>Developer Company: EDS</para> <para>Developer Phone: (919) 325-7311</para> <para>Developer Email: danny.mcnaught@eds.com</para> <para>Developed on: W2PZZ50H01</para> <para>Date: 9/10/2007</para><para>Time: 7:22:36 AM</para></term>
        ''' 		<description>Created XML Comment. </description>
        ''' 	</item>
        ''' </list>
        ''' </remarks>
        ''' <example>
        ''' </example>
        Friend Shared Function ReturnChildElementName(ByVal node As LanguageElement, ByVal elementType As LanguageElementType) As String
            For Each subNode As LanguageElement In node.Nodes
                If subNode.ElementType = elementType Then
                    Return subNode.Name
                End If
            Next
            ' Not found? Search childes
            For Each subNode As LanguageElement In node.Nodes
                If (subNode.ElementType <> LanguageElementType.Method AndAlso subNode.ElementType <> LanguageElementType.[Class]) OrElse subNode.ElementType = elementType Then
                    ' Don't go into class or methods level
                    ' Classes can only be in namespaces
                    If elementType = LanguageElementType.[Class] AndAlso subNode.ElementType <> LanguageElementType.[Namespace] Then
                        Continue For
                    End If

                    ' Go recursively through the stuff and search for the elementType
                    Dim ret As String = ReturnChildElementName(subNode, elementType)
                    If ret.Length > 0 Then
                        Return ret
                    End If
                End If
            Next
            ' foreach if ()
            Return ""
        End Function
        ' ReturnChildElementName(node, elementType)
#End Region
#Region "Find language element"
        ''' <summary>
        ''' Find language element by name, goes recursively through all elements.
        ''' </summary>
        ''' <param name="rootNode">Parameter rootNode Language element</param>
        ''' <param name="elementName">Parameter elementName As <seealso cref= "System.String"/></param>
        ''' <returns>
        ''' </returns>
        ''' <remarks>
        ''' 	<para><b>History:</b></para>
        ''' <list type="table">
        ''' 	<listheader>
        ''' 		<term>Term</term>
        ''' 		<description>Description</description>
        ''' 	</listheader>
        ''' <item>
        ''' 		<term>Developer: Danny C. McNaught<para>Developer Company: EDS</para> <para>Developer Phone: (919) 325-7311</para> <para>Developer Email: danny.mcnaught@eds.com</para> <para>Developed on: W2PZZ50H01</para> <para>Date: 9/10/2007</para><para>Time: 7:22:47 AM</para></term>
        ''' 		<description>Created XML Comment. </description>
        ''' 	</item>
        ''' </list>
        ''' </remarks>
        ''' <example>
        ''' </example>
        Friend Shared Function FindLanguageElement(ByVal rootNode As LanguageElement, ByVal elementName As String) As LanguageElement
            For nodeNum As Integer = 0 To rootNode.Nodes.Count - 1
                Dim element As LanguageElement = DirectCast(rootNode.Nodes(nodeNum), LanguageElement)
                ' Only check namespaces, classes, methods, etc.
                If element.ElementType = LanguageElementType.[Namespace] OrElse element.ElementType = LanguageElementType.[Class] OrElse element.ElementType = LanguageElementType.Method OrElse element.ElementType = LanguageElementType.[Property] OrElse element.ElementType = LanguageElementType.InitializedVariable Then
                    If [String].Compare(element.Name, elementName, True) = 0 Then
                        Return element
                    End If
                    Dim subElement As LanguageElement = FindLanguageElement(element, elementName)
                    If subElement IsNot Nothing Then
                        Return subElement
                    End If
                    ' if (element.ElementType)
                End If
            Next
            ' for (nodeNum)
            ' Not found anything here
            Return Nothing
        End Function
        ' FindLanguageElement(rootNode, elementName)
#End Region
    End Class
    ' class CodeRushHelper
End Namespace